from flask import Blueprint,request,session,render_template,jsonify
import BaseDao
# import uuid
# uuid.uuid4().hex
from redis import Redis

blu=Blueprint("login",__name__)

@blu.route('/login', methods=['post', 'get'])  # 登录
def login():
    return render_template('login.html')


@blu.route('/login/run', methods=['post', 'get'])  # 处理登录
def login_run():
    status = {'status': 200}
    name = request.form.get('username')
    pwd = request.form.get('pwd')
    sql = 'select * from user where username="{}"'.format(name)
    result = BaseDao.select_db(sql)
    if result:
        if result[0][2] == pwd:
            session.permanent = True
            session['uid'] = result[0][0]
            session['username'] = result[0][1]
        else:
            status['status'] = 1
    else:
        status['status'] = 2
    return jsonify(status)
